// pages/login/index.js
const http = require('../../utils/http.js')
const config = require('../../config.js')

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prevPage: '', //上级页面
    disabled: false //是否禁用登录按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2]; // 上一个页面
    this.setData({
      prevPage: prevPage.route
    })
  },

  /**
   * 点击登录
   */
  bindGetUserInfo: function() {
    var that = this
    that.setData({
      disabled: true
    })
    wx.getSetting({
      success: function (res) {
        // if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              wx.setStorageSync("wxuserInfo", res.userInfo)
              wx.setStorageSync("encryptedData", res.encryptedData)
              wx.setStorageSync("iv", res.iv)
              var prevPage = '/' + that.data.prevPage
              app.wxLogin(function () {
                that.setData({
                  disabled: false
                })
                wx.navigateBack({
                  url: prevPage,
                })
              })
            }
          })
        // }
      }
    })
  }
})